const http = require('http')
const request = require('request')
const cheerio = require('cheerio')

function MovieList2HTML(data) {
    return `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style type="text/css">
        .gridtable {
            font-family: verdana,arial,sans-serif;
            font-size:11px;
            color:#333333;
            border-width: 1px;
            border-color: #666666;
            border-collapse: collapse;
        }
        .gridtable th {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #dedede;
        }
        .gridtable td {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #ffffff;
        }
    </style>
</head>
<body>
    <table width="500px" id="tb" align="center" class="gridtable">
        <th>电影id</th>
        <th>电影名称</th>
        <th>电影评分</th>
    </table>
</body>
<script>
var movieList = ${JSON.stringify(data)};
var tb = document.getElementById("tb");
for(var i = 0; i < movieList.length; i++) {
    var tdNode = 
                        "<td>" +
                        movieList[i].movieId +
                        "</td>" +
                        "<td>" +
                        movieList[i].movieTitle +
                        "</td>" +
                        "<td>"  +
                        movieList[i].movieDetail + 
                        "</td>"
    var trNode = document.createElement("tr");
    trNode.innerHTML = tdNode;
    tb.appendChild(trNode);
}
</script>
</html>`
}

http.createServer((request, response) => {
    const url = 'https://trade.maoyan.com/films?showType=3'
    console.log(request.method + " " + request.url)
    if (request.url == '/') {
        //获取正在热映电影数据
        getMovies(url)
            .then((data) => {
                console.log(data);
                response.setHeader('Content-Type','text/html');
                response.end(MovieList2HTML(data))
            })
        
    }
    else if (request.url == '/getlist') {
        response.setHeader('Content-Type','application/json');
        //获取正在热映电影数据
        getMovies(url)
            .then((data) => {
                response.end(JSON.stringify(data))
            })
    }
    else {
        response.end();
    }
    
}).listen(8081)



function getMovies(url) {
    var movieArr = []

    return new Promise((resolve, reject) => {
        request(url, function (err, response, body) {
            const $ = cheerio.load(body)
            var item = $('.movie-list dd')
            item.map(function (i, val) {
                var movieObj = {}

                movieObj.movieId = $(val).find('.movie-item').children('a').attr('data-val').split(':')[1].replace('}','')
                
                movieObj.movieTitle = $(val).find('.movie-item-title').children('a').text()
                
                movieObj.movieDetail = $(val).find('div.channel-detail-orange').text()

                
                movieArr.push(movieObj)
            })

            
            if (movieArr.length > 0) {
                resolve(movieArr)
            }
        })
    })
}
